﻿
// UseAtaSmartDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "UseAtaSmart.h"
#include "UseAtaSmartDlg.h"
#include "afxdialogex.h"


#include "RAdiskCore.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CUseAtaSmartDlg 对话框



CUseAtaSmartDlg::CUseAtaSmartDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_USEATASMART_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CUseAtaSmartDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CUseAtaSmartDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_ATA_SMART, &CUseAtaSmartDlg::OnBnClickedButtonAtaSmart)
	ON_BN_CLICKED(IDC_BUTTON_GET_SYSTEM_INFO, &CUseAtaSmartDlg::OnBnClickedButtonGetSystemInfo)
END_MESSAGE_MAP()


// CUseAtaSmartDlg 消息处理程序

BOOL CUseAtaSmartDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CUseAtaSmartDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CUseAtaSmartDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CUseAtaSmartDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


typedef DWORD(WINAPI* pInit)();

void CUseAtaSmartDlg::OnBnClickedButtonAtaSmart()
{
	// TODO: 在此添加控件通知处理程序代码

	// 读取磁盘数据
	DWORD dwRet = CRADiskCore::InitAtaSmart();
	// 获取磁盘数量
	dwRet = CRADiskCore::GetAllDiskCount();

	for (int i = 0; i < dwRet; ++i)
	{
		CString strMsg;
		CString strTem;

		LPVOID pData1;
		LPVOID pData2;
		
		DWORD dwRet = 0;

		// 固件名
		dwRet = CRADiskCore::GetFirmwareRev(i, (LPCWSTR*)&pData1);
		if (dwRet == S_OK)
		{
			strTem.Format(_T("固件名：%s\t"), (LPCWSTR)pData1);
			strMsg += strTem;
		}
		// 序列号
		dwRet = CRADiskCore::GetSerialNumber(i, (LPCWSTR*)&pData1);
		if (dwRet == S_OK)
		{
			strTem.Format(_T("序列号：%s\n"), (LPCWSTR)pData1);
			strMsg += strTem;
		}
		// 写入量
		dwRet = CRADiskCore::GetHostWrites(i, (LPDWORD)&pData1);
		if (dwRet == S_OK)
		{
			strTem.Format(_T("总写入：%dGB\t"), (DWORD)pData1);
			strMsg += strTem;
		}
		// 读取量
		dwRet = CRADiskCore::GetHostReads(i, (LPDWORD)&pData1);
		if (dwRet == S_OK)
		{
			strTem.Format(_T("总读取：%dGB\n"), (DWORD)pData1);
			strMsg += strTem;
		}
		// 通电次数
		dwRet = CRADiskCore::GetPowerOnCount(i, (LPDWORD)&pData1);
		if (dwRet == S_OK)
		{
			strTem.Format(_T("通电次数：%d次\t"), (DWORD)pData1);
			strMsg += strTem;
		}
		// 总通电时间
		dwRet = CRADiskCore::GetPowerOnHours(i, (LPDWORD)&pData1);
		if (dwRet == S_OK)
		{
			strTem.Format(_T("总通电时间：%d小时\n"), (DWORD)pData1);
			strMsg += strTem;
		}
		// 硬盘接口类型
		dwRet = CRADiskCore::GetInterface(i, (LPCWSTR*)&pData1);
		if (dwRet == S_OK)
		{
			strTem.Format(_T("硬盘接口类型：%s\t"), (LPCWSTR)pData1);
			strMsg += strTem;
		}
		// 对应盘符
		dwRet = CRADiskCore::GetDriverMap(i, (LPCWSTR*)&pData1);
		if (dwRet == S_OK)
		{
			strTem.Format(_T("对应盘符：%s\n"), (LPCWSTR)pData1);
			strMsg += strTem;
		}
		// 硬盘接口主版本号
		dwRet = CRADiskCore::GetMajorVersion(i, (LPCWSTR*)&pData1);
		if (dwRet == S_OK)
		{
			strTem.Format(_T("硬盘接口主版本号：%s\t"), (LPCWSTR)pData1);
			strMsg += strTem;
		}
		// 硬盘接口次版本号
		dwRet = CRADiskCore::GetMinorVersion(i, (LPCWSTR*)&pData1);
		if (dwRet == S_OK)
		{
			strTem.Format(_T("硬盘接口次版本号：%s\n"), (LPCWSTR)pData1);
			strMsg += strTem;
		}
		// 硬盘当前使用协议
		dwRet = CRADiskCore::GetCurTransMode(i, (LPCWSTR*)&pData1);
		if (dwRet == S_OK)
		{
			strTem.Format(_T("硬盘当前使用协议：%s\t"), (LPCWSTR)pData1);
			strMsg += strTem;
		}
		// 硬盘最高支持协议
		dwRet = CRADiskCore::GetCurMaxMode(i, (LPCWSTR*)&pData1);
		if (dwRet == S_OK)
		{
			strTem.Format(_T("硬盘最高支持协议：%s\t"), (LPCWSTR)pData1);
			strMsg += strTem;
		}
		// 硬盘是否是SSD
		dwRet = CRADiskCore::IsSSD(i, (PBOOL)&pData1);
		if (dwRet == S_OK)
		{
			strTem.Format(_T("硬盘是否是SSD：%d（1代表是，0代表不是）\n"), (BOOL)pData1);
			strMsg += strTem;
		}
		// 硬盘温度
		dwRet = CRADiskCore::GetTemperature(i, (LPDWORD)&pData1);
		if (dwRet == S_OK)
		{
			strTem.Format(_T("硬盘温度：%d°\t"), (DWORD)pData1);
			strMsg += strTem;
		}
		// 硬盘寿命
		dwRet = CRADiskCore::GetLife(i, (LPDWORD)&pData1);
		if (dwRet == S_OK)
		{
			strTem.Format(_T("硬盘寿命：%d\n"), (DWORD)pData1);
			strMsg += strTem;
		}
		// 支持的模式
		dwRet = CRADiskCore::GetSupportMode(i, (LPCWSTR*)&pData1);
		if (dwRet == S_OK)
		{
			strTem.Format(_T("支持的模式：%s\n"), (LPCWSTR)pData1);
			strMsg += strTem;
		}
		// 硬盘信息
		dwRet = CRADiskCore::GetModelSerial(i, (LPCWSTR*)&pData1);
		if (dwRet == S_OK)
		{
			strTem.Format(_T("硬盘信息：%s\n"), (LPCWSTR)pData1);
			strMsg += strTem;
		}
		// 硬盘容量
		dwRet = CRADiskCore::GetTotalDiskSize(i, (LPDWORD)&pData1);
		if (dwRet == S_OK)
		{
			strTem.Format(_T("硬盘容量：%dGB\n"), (DWORD)pData1/1024);
			strMsg += strTem;
		}
		
		// 扩展信息
		dwRet = CRADiskCore::GetAttributeCount(i, (LPDWORD)&pData1);
		if (dwRet == S_OK)
		{
			DWORD dwCount = (DWORD)pData1;
			if (dwCount != 0)
			{
				strMsg += _T("扩展信息：\n");
				for (int j = 0; j < dwCount; ++j)
				{
					dwRet = CRADiskCore::GetAttributeData(i, j, (LPCWSTR*)&pData1, (LPCWSTR*)&pData2);

					if (dwRet == S_OK)
					{
						strTem.Format(_T("\t%s\t%s\n"), (LPCWSTR)pData1,(LPCWSTR)pData2);
						strMsg += strTem;
					}
				}
			}
		}
		if (!strMsg.IsEmpty())
			MessageBox(strMsg);
	}
}


void CUseAtaSmartDlg::OnBnClickedButtonGetSystemInfo()
{
	// TODO: 在此添加控件通知处理程序代码




}
